/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hutool.core.collection.CollectionUtil;
import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.comac.ins.isfd.domain.bo.FastenerInfoBo;
import com.comac.ins.isfd.domain.bo.FastenerModelBo;
import com.comac.ins.isfd.domain.bo.query.FastenerInfoAllowableValueQueryBo;
import com.comac.ins.isfd.domain.bo.query.FastenerModelAllowableValueQueryBo;
import com.comac.ins.isfd.service.IFastenerInfoService;
import com.comac.ins.isfd.service.IFastenerModelService;
import lombok.extern.slf4j.Slf4j;

/**
 * 紧固件service实现类
 *
 * @author wujialin
 * @date 2025-6-7
 */
@Slf4j
@Service
public class IFastenerInfoServiceImpl extends ServiceImpl<FastenerInfoBo> implements IFastenerInfoService {
    @Autowired
    private IFastenerModelService iFastenerModelService;

    @Override
    public List<FastenerInfoBo> queryPageList(FastenerInfoAllowableValueQueryBo bo) {
        FastenerModelAllowableValueQueryBo fastenerModelAllowableValueQueryBo = new FastenerModelAllowableValueQueryBo();
        fastenerModelAllowableValueQueryBo.set_id(bo.getFasteneraModelId());
        fastenerModelAllowableValueQueryBo.setUnit(bo.getUnit());
//        fasteneraModelAllowableValueQueryBo.setFastenerGrade(bo.getFastenerGrade());
//        fasteneraModelAllowableValueQueryBo.setSheetName(bo.getSheetName());
        List<FastenerModelBo> fastenerModelBoList = iFastenerModelService.queryPageList(fastenerModelAllowableValueQueryBo);
        if (CollectionUtil.isNotEmpty(fastenerModelBoList)) {
            FastenerModelBo fastenerModelBo = fastenerModelBoList.get(0);
            List<String> fastenerInfoList = fastenerModelBo.getFastenerInfo();
            // 构造查询参数
            LambdaQueryChainWrapper<FastenerInfoBo> queryChainWrapper = this.lambdaQuery();
            if ("metric".equals(bo.getUnit())) {
                queryChainWrapper
                    .eq(StringUtils.isNotBlank(bo.getDiameterCode()), FastenerInfoBo::getDiameterCode, bo.getDiameterCode())
                    .gte(StringUtils.isNotBlank(bo.getThicknessMMStart()), FastenerInfoBo::getThicknessMM, bo.getThicknessMMStart())
                    .lte(StringUtils.isNotBlank(bo.getThicknessMMEnd()), FastenerInfoBo::getThicknessMM, bo.getThicknessMMEnd())
                    .gte(StringUtils.isNotBlank(bo.getThicknessInchesStart()), FastenerInfoBo::getThicknessInches, bo.getThicknessInchesStart())
                    .lte(StringUtils.isNotBlank(bo.getThicknessInchesEnd()), FastenerInfoBo::getThicknessInches, bo.getThicknessInchesEnd())
                    .in(CollectionUtil.isNotEmpty(fastenerInfoList), FastenerInfoBo::get_id, fastenerInfoList);

            } else {
                queryChainWrapper
                    .eq(StringUtils.isNotBlank(bo.getDiameterCode()), FastenerInfoBo::getDiameterCode, bo.getDiameterCode())
                    .gte(StringUtils.isNotBlank(bo.getThicknessMMStart()), FastenerInfoBo::getThicknessMM, bo.getThicknessMMStart())
                    .lte(StringUtils.isNotBlank(bo.getThicknessMMEnd()), FastenerInfoBo::getThicknessMM, bo.getThicknessMMEnd())
                    .gte(StringUtils.isNotBlank(bo.getThicknessInchesStart()), FastenerInfoBo::getThicknessInches, bo.getThicknessInchesStart())
                    .lte(StringUtils.isNotBlank(bo.getThicknessInchesEnd()), FastenerInfoBo::getThicknessInches, bo.getThicknessInchesEnd())
                    .in(CollectionUtil.isNotEmpty(fastenerInfoList), FastenerInfoBo::get_id, fastenerInfoList);

            }

            List<FastenerInfoBo> fastenerInfoBoList = this.list(queryChainWrapper);
            return fastenerInfoBoList;
        }
        return List.of();
    }
}
